﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HB.Base;
namespace SimpleWatch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            speed.OnSpeedChanged += Speed_OnSpeedChanged;
        }

        private void Speed_OnSpeedChanged(double obj)
        {
            this.Invoke(new Action(() =>
            {
                labelSpeed.Text = $"{obj.ToString("F1")}/s";

                labelSpeed.ForeColor = obj > 100 ? Color.DarkRed : Color.DarkGreen;

                labelTimes.Text = GetTimeText(CallTimes);
            }));
        }
        string GetTimeText(ulong times)
        {
            string unit = "";
            double num = times;
            if (num > 1000) { unit = "K"; num /= 1000; }
            if (num > 1000) { unit = "M"; num /= 1000; }
            return $"{num.ToString("f3")}{unit}";

        }
        ulong CallTimes { get; set; }
        AverageSpeed speed = new AverageSpeed(3);
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            if (fb.ShowDialog() == DialogResult.OK)
            {
                AddDir(fb.SelectedPath);
            }
        }
        void AddDir(string path)
        {
            DirWatch w = new DirWatch(path, () =>
            {
                CallTimes += 1;
                speed.Log();
            });
            listBox1.Items.Add(path);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var dirs = System.IO.DriveInfo.GetDrives();
            foreach (var d in dirs)
            {
                AddDir(d.RootDirectory.ToString());
            }
        }
    }
}
